# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openshadinglanguage
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=1.7.3
pkgrel=1
pkgdesc="Advanced shading language for production GI renderers (mingw-w64)"
arch=('any')
url="https://github.com/imageworks/OpenShadingLanguage/"
license=("custom")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-ilmbase"
         "${MINGW_PACKAGE_PREFIX}-intel-tbb"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-openimageio"
        )
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-clang35"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
source=(https://github.com/imageworks/OpenShadingLanguage/archive/Release-${pkgver}.tar.gz
        0002-dont-add-flex-include-dir.patch
        0003-fix-OIIO-windows.patch
        0004-fix-installing-docs.patch
        0005-Fix-cast-from-void-ptr.patch
        0007-Fix-bad-casts-with-intptr_t.patch)
sha256sums=('ef33cb968a1cc029460dd9c2e6195a28179386ef0aeb4c2300b756fe5aa3f0b8'
            'c9e02624cf6e607118f1f7fd8bf8bda9af5103afde5770a11fa5ccf49916fc5b'
            '597241b0d10799b9d38629f64a98c2cc54e48dbbf9b893fdd078cb40da019240'
            '08bee2c1bbe45a1e8a2face08bdf1bc28f63dd22fe2e2df9fde7a136b77561de'
            '541cc1eb0c506e253de2f26af37dc6e35de891a940906c6e33a2f4774a4a844e'
            'd3e6d2bdd059ec9b1581b02c9f736f26a06e1189929c6276d1d7d81204bb3535')

prepare() {
  cd ${srcdir}/OpenShadingLanguage-Release-${pkgver}
  patch -p1 -i ${srcdir}/0002-dont-add-flex-include-dir.patch
  patch -p1 -i ${srcdir}/0003-fix-OIIO-windows.patch
  patch -p1 -i ${srcdir}/0004-fix-installing-docs.patch
  patch -p1 -i ${srcdir}/0005-Fix-cast-from-void-ptr.patch
  patch -p1 -i ${srcdir}/0007-Fix-bad-casts-with-intptr_t.patch
}

build() {
  export OPENIMAGEIOHOME=${MINGW_PREFIX}
  export LLVM_DIRECTORY=${MINGW_PREFIX}
  local _extra_libs=$(${MINGW_PREFIX}/bin/llvm-config --system-libs)
  CFLAGS+=" -std=c++11 -D_USE_MATH_DEFINES"
  CXXFLAGS+=" -std=c++11 -DTINYFORMAT_ALLOW_WCHAR_STRINGS -D_USE_MATH_DEFINES"

  [[ -d build-${MINGW_CHOST} ]] && rm -r build-${MINGW_CHOST}
  mkdir build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DILMBASE_CUSTOM=True \
    -DILMBASE_CUSTOM_INCLUDE_DIR=${MINGW_PREFIX}/include \
    -DILMBASE_CUSTOM_LIB_DIR=${MINGW_PREFIX}/lib \
    -DILMBASE_CUSTOM_LIBRARIES="Half Iex Imath IlmThread" \
    -DEXTRA_OSLEXEC_LIBRARIES="${_extra_libs}" \
    -DLLVM_STATIC=1 \
    -DOSL_BUILD_CPP11=1 \
    ../OpenShadingLanguage-Release-${pkgver}

    #-DCMAKE_CXX_FLAGS="-DTINYFORMAT_ALLOW_WCHAR_STRINGS"
    #-DVERBOSE=1

  # $PWD/src/liboslcomp PATH addition needed for running oslc.exe during the build (for liboslcomp.dll)
  # ${MINGW_PREFIX}/bin PATH addition needed for finding libboost_system-mt.dll when running oslc.exe.
  PATH=${PWD}/src/liboslcomp:${MINGW_PREFIX}/bin:"${PATH}" make #VERBOSE=1
}

package() {
  cd ${srcdir}//build-${MINGW_CHOST}

  make DESTDIR=${pkgdir} install

  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/OSL/
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}
  
  install -Dm644 "${srcdir}/OpenShadingLanguage-Release-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
